package com.das.mechanic_base.widget;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.main.HomeColorBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.List;

public class X3BottomTyreBrandDialog extends X3BaseBottomSheetDialog implements View.OnClickListener {

    private TextView tv_cancel;
    private WheelView<String> wv_one;
    private LinearLayout ll_select;
    private ImageView iv_select;
    private TextView tv_affirm;

    private String cameraSn;

    private boolean isSelect;
    private String answer;

    public X3BottomTyreBrandDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.x3_bottom_tyre_brand_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(true);
        setCancelable(true);
        initView();
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        tv_cancel = getView(R.id.tv_cancel);
        wv_one = getView(R.id.wv_one);
        ll_select = getView(R.id.ll_select);
        iv_select = getView(R.id.iv_select);
        tv_affirm = getView(R.id.tv_affirm);

        wv_one.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
        wv_one.setSoundEffectResource(R.raw.x3_button_choose);
        wv_one.setSoundEffect(true);
        wv_one.setPlayVolume(0.5f);

        tv_cancel.setOnClickListener(this);
        ll_select.setOnClickListener(this);
        tv_affirm.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_bottom_tyre_brand_dialog;
    }

    /**
     * 请求轮胎轮毂数据
     * @param questionToSystemSn
     */
    public void setQuestionSystemSn(String questionToSystemSn,String answer) {
        this.cameraSn = questionToSystemSn;
        this.answer = answer;
        NetWorkHttp.getApi().showTyreBrand()
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<HomeColorBean>>() {
                    @Override
                    protected void onDone(List<HomeColorBean> mList) {
                        if (X3StringUtils.isListEmpty(mList)) {
                            return;
                        }
                        List<String> tyreList = new ArrayList<>();
                        for (int i = 0; i < mList.size(); i++) {
                            tyreList.add(mList.get(i).getLabel() + "");
                        }
                        if (wv_one != null) {
                            wv_one.setData(tyreList);
                        }
                        if (X3StringUtils.isEmpty(answer)) {
                            return;
                        }
                        if (!tyreList.contains(answer)) {
                            return;
                        }
                        wv_one.setSelectedItemPosition(tyreList.indexOf(answer));
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.ll_select) {
            if (isSelect) {
                iv_select.setImageResource(R.mipmap.x3_car_no_select);
            } else {
                iv_select.setImageResource(R.mipmap.x3_car_select);
            }
            isSelect = !isSelect;
        } else if (id == R.id.tv_affirm) {
            dismiss();
            if (iOnSelectTyreBrand != null) {
                iOnSelectTyreBrand.iOnSelectBrandTyre(wv_one.getSelectedItemData(), cameraSn, isSelect);
            }
        }
    }

    IOnSelectTyreBrand iOnSelectTyreBrand;

    public void setiOnSelectTyreBrand(IOnSelectTyreBrand iOnSelectTyreBrand) {
        this.iOnSelectTyreBrand = iOnSelectTyreBrand;
    }

    public interface IOnSelectTyreBrand{
        void iOnSelectBrandTyre(String name,String sn,boolean isAll);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息选择轮胎品牌弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息选择轮胎品牌弹窗");
    }
}
